define([
  'jquery',
  'jqueryui',
  'underscore',
  'backbone',
  'lightboxme',
  'multiselect',
  'multiselectfilter',
  'msgbox',
  'text!templates/profiles/profileTemplate.html'
], function($,JQueryUI, _, Backbone,LightBoxMe,Multiselect,Multiselectfilter,MsgBox,profileTemplate){

  var ProfileView = Backbone.View.extend({
    el: $(".mainpanel"),
    initialize: function(){
    	//_.bindAll(this, "changed");
 
    },
    events: { "click #searchProfileDetails" :"showProfileDetails" ,
    	 "click #addProfile" :"addProfile" ,
    	 "change #selectExam" :"addExaminations",
    	 "click #saveCustomProfile": "saveCustomProfile"
    	/*"change #sprofiles" :"addsProfile",
    		"change #sexaminations" :"addsType"*/} ,
    render: function(){
    	var self=this;
    	  this.$el.html(profileTemplate);
        $("input#profileName").autocomplete({
    	    source: "/getProfilesForDoc",
    	    minLength: 2,
    	    select: function(e,ui){
    	    	//self.changedSearchAC("complaint",ui);
    	    }
    	});
      	 $.ajax({
 	        type: "GET",
 	        url: "/getExaminationList",
 	        success: function(response){
 	        	self.examList = response;
 	           var data = {
 	                  _: _ ,
 	                  examinations:response
 	                };
 	  	      var compiledTemplate = _.template( profileTemplate, data );
 	  	      self.$el.html(compiledTemplate);

 	  	    
 	        }
 	    });

    },
    showProfileDetails: function(){
    	      	 $.ajax({
	        type: "GET",
	        url: "/getProfilesForDocSearch?term="+$("#profileName").val(),
	        success: function(response){
	        	if(response.length==0){
	        		 $.msgBox({
						    title:"Appment",
						    content:"No profile found. You can go ahead and add with this name.",
						    type:"info",
						    success: function (result) {
						    	$("#examcombo").show();
						  	      $("#selectExam").multiselect({
						  	    	noneSelectedText: "Examinations"
						  	    	}).multiselectfilter();
						    }
						});
	        	}else{
	        		$("#examcombo").show();
			  	      $("#selectExam").multiselect({
			  	    	noneSelectedText: "Examinations"
			  	    	}).multiselectfilter();
			  		var html="";
					for(var i in response){
						html=html+"<tr><td><input type=\"checkbox\" name=\"checkexam\" checked value="+ response[i].id +">"+response[i].examination.examinationName+"</td><tr>";
					}
					$('.tableexam').html(html);
  
	        	}
	        
	        }
	    });

    },
    addProfile:function(){
     	 $.ajax({
 	        type: "GET",
 	        url: "/getProfilesForDocSearch?term="+$("#profileName").val(),
 	        success: function(response){
 	        	if(response.length>0){
 	        		 $.msgBox({
 						    title:"Appment",
 						    content:"You have already created a profile by this name.Please choose a different name.",
 						    type:"info",
 						    success: function (result) {}
 						});
 	        	}else{
 	        		$("#examcombo").toggle();
 			  	      $("#selectExam").multiselect({
 			  	    	noneSelectedText: "Examinations"
 			  	    	}).multiselectfilter();
 	        	}
 	        
 	        }
 	    });
    
    },    
    addExaminations: function(){
    	var selected = $("#selectExam").val();
		var text="";
		var html="";
		for(var i in this.examList){
			for(var j in selected){
				if(selected[j]==this.examList[i].id){
					html=html+"<tr><td><input type=\"checkbox\" name=\"checkexam\" checked value="+ selected[j] +">"+this.examList[i].examinationName+"</td><tr>";
					break;
				}
			}
		}
		$('.tableexam').html(html);
    },
    saveCustomProfile: function(){
    	var exams =[];
		  $('.tableexam').find('tr').each(function () {
		        var row = $(this);
		        if (row.find('input[type="checkbox"]').is(':checked')) {
		        	exams.push(Number(row.find('input[type="checkbox"]').val()));
		            
		        }
		    });
    	  console.log(exams);
    	  $.ajax({
			  type: "POST",
			  url: '/saveCustomProfile',
			  dataType:"json",
			  contentType:"application/json; charset=utf-8",
			  data: JSON.stringify({
				  profileName: $('#profileName').val() ,
				  examIds:exams
			  }),
			  success: function(){
		      		 $.msgBox({
						    title:"Appment",
						    content:"Saved Successfully!!",
						    type:"info"
						});
  
			  },
			  dataType: 'json'
			});
    }
  });

  return ProfileView;
});
